<?php

include_once './Data/BDDManager.inc';
include_once 'LogManager.inc';

class Partie {

    var $id;
    var $Joueur1;
    var $Joueur2;
    var $MotDePasse;
    var $HRestantJ1;
    var $MRestantJ1;
    var $SRestantJ1;
    var $HRestantJ2;
    var $MRestantJ2;
    var $SRestantJ2;
    var $JoueurCourant;
    var $derniereUpdate;
    var $estEnPause;
    var $bdd;

    public function __construct($aId, $aBDD) {
        $this->id  = $aId;
        $this->bdd = $aBDD;
    }

    public function changeJoueurEnCours($joueur, $pause) {
        $req = 'UPDATE `warmaclockparties`
				SET `JoueurEnCours` = \'' . $joueur . '\',
				    `DerniereUpdate` = ' . time() . ',
				    `estEnPause` = ' . $pause . '  
				WHERE `warmaclockparties`.`idPartie` = ' . $this->id/* .'
          AND (`DerniereUpdate` - '.time().') > 5' */;
        //echo $req;
        $this->bdd->exec($req);
    }

    public function JoueurByIdConnected() {
        if (isset($_SESSION['idConnectedPlayeur'])) {
            if ($_SESSION['idConnectedPlayeur'] == 'J1') {
                return $this->Joueur1;
            }if ($_SESSION['idConnectedPlayeur'] == 'J2') {
                return $this->Joueur2;
            }
        }
        return "NC";
    }

    public function updatePause($pause) {
        $req = 'UPDATE `warmaclockparties`
				SET `DerniereUpdate` = ' . time() . ',
				    `estEnPause` = ' . $pause . '  
				WHERE `warmaclockparties`.`idPartie` = ' . $this->id/* .'
          AND (`DerniereUpdate` - '.time().') > 5' */;
        LogManager::logMsg($req);
        $this->bdd->exec($req);
    }

    public function updateAll($pause, $clock1H, $clock1M, $clock1S, $clock2H, $clock2M, $clock2S) {
        $req = 'UPDATE `warmaclockparties`
				SET `HRestantJ1`=' . $clock1H . ',
					`MRestantJ1`=' . $clock1M . ',
					`SRestantJ1`=' . $clock1S . ',
					`HRestantJ2`=' . $clock2H . ',
					`MRestantJ2`=' . $clock2M . ',
					`SRestantJ2`=' . $clock2S . ',
					`DerniereUpdate`=' . time() . ',
					`estEnPause`=' . $pause . '  
				WHERE `warmaclockparties`.`idPartie` = ' . $this->id/* .'
          AND (`DerniereUpdate` - '.time().') > 5' */;
        //echo $req;
        $this->bdd->exec($req);
    }

    public function update() {
        $req    = "SELECT *
			  FROM `warmaclockparties`
			  WHERE idPartie=" . $this->id;
        $result = $this->bdd->query($req);
        if ($result) {
            $row = $result->fetch();
        }
        if (isset($row)) {
            $this->Joueur1        = $row[1];
            $this->Joueur2        = $row[2];
            $this->MotDePasse     = $row[3];
            $this->HRestantJ1     = $row[4];
            $this->MRestantJ1     = $row[5];
            $this->SRestantJ1     = $row[6];
            $this->HRestantJ2     = $row[7];
            $this->MRestantJ2     = $row[8];
            $this->SRestantJ2     = $row[9];
            $this->JoueurCourant  = $row[10];
            $this->derniereUpdate = $row[11];
            $this->estEnPause     = $row[12];
        }
    }

    public function updateFast() {
        $req    = "SELECT JoueurEnCours, estEnPause
			  FROM `warmaclockparties`
			  WHERE idPartie=" . $this->id;
        $result = $this->bdd->query($req);
        if ($result) {
            $row = $result->fetch();
        }
        if (isset($row)) {
            $this->JoueurCourant = $row[0];
            $this->estEnPause    = $row[1];
        }
    }

}

?>